import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 跳转了重复路由
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err)
}

const routes = [
  {
    path: '/',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/index/index.vue'),
  },
  {
    path: '/list',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/list.vue'),
  },
  {
    path: '/day1-airport',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/day1-airport.vue'),
  },
  {
    path: '/day1-high',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/day1-high.vue'),
  },
  {
    path: '/day2-airport',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/day2-airport.vue'),
  },
  {
    path: '/day2-high',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/day2-high.vue'),
  },
  {
    path: '/hotels',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/hotels.vue'),
  },
  {
    path: '/survey',
    meta: {
      title: '2024南区全新BMW 5系&创新纯电动BMW i5向新之旅',
    },
    component: () => import('@/views/survey.vue'),
  },
  // 错误路由404页面
  {
    path: '*',
    redirect: '/404',
  },
  {
    path: '/404',
    meta: {
      title: '404',
    },
    name: 'IndexHome404',
    component: () => import('@/views/404/index.vue'),
  },
]
const router = new VueRouter({
  routes,
  mode:'history',
})

/**
 * 进入路由前
 * to: Route: 即将要进入的目标 路由对象
 * from: Route: 当前导航正要离开的路由
 * next: Function: 一定要调用该方法来 resolve 这个钩子
 */
router.beforeEach((to, from, next) => {
  // 获取页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  }
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0

  next()
})

export default router
